var setting = {
	check: {
		enable: true,
		chkStyle: "radio",
		radioType: "all"
	},
	async: {
		enable: true,
		url: getUrl
	},
	data: {
		simpleData: {
		  enable: true
	    }
	},
	view: {
		expandSpeed: ""
	},
	callback: {
		beforeExpand: beforeExpand,
		onAsyncSuccess: onAsyncSuccess,
		onAsyncError: onAsyncError
	}
};
function getUrl(treeId, treeNode) {
	var sign=treeNode.id.substr(0,1);
	var cid=treeNode.id.substr(1);
	if(sign=='C'){
		return "../base/queryTree_Ban.action?cid="+cid;
	}else if(sign=="B"){
		return "../base/queryTree_Unit.action?bid="+cid;
	}else if(sign=="U"){
		return "../base/queryTree_Room.action?uid="+cid;
	}
};
function beforeExpand(treeId, treeNode) {
	if (!treeNode.isAjaxing) {
		ajaxGetNodes(treeNode, "refresh");
		return true;
	} else {
		alert("正在下载数据中，请稍后展开节点。。。");
		return false;
	}
};
function ajaxGetNodes(treeNode, reloadType) {
	var zTree = $.fn.zTree.getZTreeObj("menup");
	if (reloadType == "refresh") {
		treeNode.icon = "../script/ztree/css/zTreeStyle/img/loading.gif";
		zTree.updateNode(treeNode);
	}
	zTree.reAsyncChildNodes(treeNode, reloadType, true);
};
function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
	var zTree = $.fn.zTree.getZTreeObj("menup");
	alert("异步获取数据出现异常。");
	treeNode.icon = "";
	zTree.updateNode(treeNode);
};
function onAsyncSuccess(event, treeId, treeNode, msg) {
	var zTree = $.fn.zTree.getZTreeObj("menup"),
	totalCount = treeNode.count;
	treeNode.icon = "";
	zTree.updateNode(treeNode);
	zTree.selectNode(treeNode.children[0]);
};

$(document).ready(function(){
	$.post("../base/query_RoomInfo.action",{},function(result){
		$.fn.zTree.init($("#menup"), setting, result.communitys);
//		var nodeId=$("#currentid").val();
//		if(nodeId!=null && nodeId!='' && nodeId!='undefined'){
//			alert(nodeId);
//			var zTree = $.fn.zTree.getZTreeObj("menup");
//			var node = zTree.getNodeByParam("id",nodeId);
//			alert(node);
//			zTree.expandNode(node, true, true, true,true);
//		}
	});
	
});
/*新增*/
function add(){
	var treeObj = $.fn.zTree.getZTreeObj("menup");
	var sNodes = treeObj.getCheckedNodes(true);
	if(sNodes.length>0){
		var rid=sNodes[0].id.substr(1);
		window.location.assign("../base/load_RoomInfo.action?rid="+rid);
	}else{
		alert("请选择房间号.")
	}
};
/*删除*/
function remove(rid){
	$('#myModal').modal('show');
	$.post("../base/remove_RoomInfo.action",{"rid":rid}, function(result) {
		if(result.msg=="succ"){
			$('#myModal').modal('hide');
			$("#alertinfo").html("房屋信息删除成功。");
			$("#closewin").bind("click",function(){
				$("#closewin").unbind("click");
				$('#myinfo').modal('hide');
				window.location.reload();
			});
			$('#myinfo').modal('show');
		}else{
			$('#myModal').modal('hide');
			$("#alertinfo").html(result.msg);
			$('#myinfo').modal('show');
		}
	});
}